<template>
  <div class="dashboard-editor-container">
    <div>
      <el-card>
        <el-form :model="ruleForm" style="margin-top:10px;" size="mini" label-position="top" ref="ruleForm">
          <el-row :gutter="10">
            <!-- <el-col :lg="4" :md="6">
              <el-form-item label="批次号" prop="BATCHNO">
              <el-input size="mini" v-model="ruleForm.BATCHNO" placeholder="请选择批次号"></el-input>
            </el-form-item>
            </el-col> -->
            <!-- <el-col :lg="4" :md="6">
              <el-form-item label="渠道名称" prop="CHANNELTYPE">
              <el-select v-model="ruleForm.CHANNELTYPE" size="mini" clearable placeholder="请选择渠道">
                <el-option v-for="item in channelOption" :key="item.FIELDKEY" :label="item.FIELDVALUE"
                  :value="item.FIELDKEY"></el-option>
              </el-select>
            </el-form-item>
            </el-col> -->
            <el-col :lg="4" :md="6">
              <el-form-item label="抽样方式" prop="SAMPLFLAG">
                <el-select v-model="ruleForm.SAMPLFLAG" size="mini" placeholder="请选择抽样方式">
                  <el-option label="手动抽样" value="1"></el-option>
                  <el-option label="自动抽样" value="0"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="开始日期" prop="BEGINDATE">
                <el-date-picker size="mini" style="width:100%;" v-model="ruleForm.BEGINDATE" type="date"
                  value-format="yyyyMMdd" :clearable='false' placeholder="开始日期" :picker-options="pickerOptionsStart"
                  @change="changeStart">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="结束日期" prop="ENDDATE">
                <el-date-picker style="width:100%;" size="mini" v-model="ruleForm.ENDDATE" type="date"
                  value-format="yyyyMMdd" placeholder="结束日期" :picker-options="pickerOptionsEnd" @change="changeEnd"
                  :clearable='false'>
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :lg="12" :md="12" class="searchBtnMarginTop" >
              <el-button type="primary" icon="el-icon-search" :loading="loading" size="mini" @click="querySummary">查询
              </el-button>
              <el-button type='warning' size='mini' icon='el-icon-refresh' @click="clearForm('ruleForm')">重置</el-button>
              <el-button type='success' size='mini' icon='el-icon-s-tools' @click="openAutoSample()">触发自动抽样
              </el-button>

            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
    <el-row style="margin-top:20px">
      <el-col>
        <el-card>
          <el-table :data="tableData" border highlight-current-row ref="tableData">
            <el-table-column prop='BATCHNO' label='批次号' align="center" width="100"></el-table-column>
            <el-table-column prop='CHANNELTYPE' label='渠道' align="center" width="100"></el-table-column>
            <el-table-column prop='OPERAWORKDATE' label='抽样日期' align="center" width="100"></el-table-column>
            <el-table-column prop='SAMPLFLAG' label='抽样方式' :formatter="dataFormat" align="center" width="130">
            </el-table-column>
            <el-table-column prop='SUCCESSNUM' label='笔数' align="center"></el-table-column>
            <el-table-column label='操作' align="center">
              <template slot-scope="scope">
                <div>
                  <el-button type="primary" size="mini" @click="openAutoSummary(scope.row)">查看明细</el-button>
                </div>
              </template>
            </el-table-column>
            <el-table-column label='触发' align="center">
              <template slot-scope="scope">
                <div>
                  <el-button type='success' size='mini'  v-if="scope.row.SAMPLFLAG == '0'" icon='el-icon-s-tools' @click="openUpdate(scope.row,'0')">触发自动抽样
                  </el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div style="text-align:right;margin-top:20px;" v-show="this.tableData">
            <el-pagination :current-page="currentPage" background @size-change="handleSizeChange"
              :page-sizes="[10,25,50,100]" :page-size="pageSize" layout="total,sizes, prev, pager, next, jumper"
              :total="tableTotal" @current-change="changePage"></el-pagination>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog width="50%" title="触发自动抽样" :close-on-click-modal="false" :visible.sync="updateIsShow" append-to-body
      @close="closeDialog">
      <el-row>
        <el-form :model="updateForm" size="mini" label-width="100px" ref="updateForm">
          <el-col :span="12">
            <el-form-item label="开始日期" prop="BEGINDATE">
              <el-date-picker size="mini" style="width:100%;" v-model="updateForm.BEGINDATE" type="date"
                value-format="yyyyMMdd" :clearable='false' placeholder="开始日期" :picker-options="pickerOptionsStart1"
                @change="changeStart1">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="ENDDATE">
              <el-date-picker style="width:100%;" size="mini" v-model="updateForm.ENDDATE" type="date"
                value-format="yyyyMMdd" placeholder="结束日期" :picker-options="pickerOptionsEnd1" @change="changeEnd1"
                :clearable='false'>
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-table :data="batchOption" border highlight-current-row>
              <el-table-column prop='BATCHNO' label='批次号' align="center"></el-table-column>
              <el-table-column prop='BATCHNODATE' label='批次日期' align="center" ></el-table-column>
              <el-table-column prop='CHANNELTYPE' label='渠道' align="center"></el-table-column>
              <el-table-column label='触发' align="center">
              <template slot-scope="scope">
                <div>
                  <el-button type='success' size='mini' icon='el-icon-s-tools' @click="openUpdate(scope.row,'1')">触发</el-button>
                </div>
              </template>
            </el-table-column>
            </el-table>
          </el-col>
          <el-col :span="24">
            <el-form-item>
              <!-- <el-button type="primary" @click="submitForm('updateForm')">触发</el-button> -->
              <!-- <el-button @click="updateIsShow = false">取消</el-button> -->
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
  // 发送接口
  import request from "@/common/utils/requestQC";
  import dictionary from '@/message/wfm/baseData/dictionaryManage/dictionaryQuery'
  import requestWFM from "@/common/utils/requestWFM";
  import summary from '@/message/QCManage/sampleManage/sampleSummary/sampleSummary';
  import batch from '@/message/QCManage/sampleManage/sampleSummary/batchQuery';
  import triggerSample from "@/message/QCManage/sampleManage/triggerSample/triggerSample";
  //批量上传
  export default {
    name: '',
    data() {
      return {
        pickerOptionsStart1: {
          disabledDate: (time) => {
            return time.getTime() > Date.now()
          }
        },
        pickerOptionsEnd1: {
          disabledDate: (time) => {
            return time.getTime() > Date.now()
          }
        },
        updateForm: {
          BEGINDATE: this.getDay(-30),
          ENDDATE: this.getDay(0)
        },
        updateIsShow: false,
        batchOption: [], //采集批次号
        channelOption: [],
        pickerOptionsStart: {
          disabledDate: (time) => {
            return time.getTime() > Date.now()
          }
        },
        pickerOptionsEnd: {
          disabledDate: (time) => {
            return time.getTime() > Date.now()
          }
        },
        tableTotal: 0,
        pageSize: 10,
        currentPage: 1, //表格当前页
        tableData: [],
        ruleForm: {
          SAMPLFLAG: '', //抽样标识
          BEGINDATE: this.getDay(-6),
          ENDDATE: this.getDay(0),
        },
        loading: false,
      }
    },
    computed: {},
    created() {},
    mounted() {
      this.querySummary()
      this.channelQuery()
      // this.batchQuery()
    },
    watch: {},
    methods: {
      closeDialog() {
        if (this.$refs['updateForm'] != undefined) {
          this.$refs['updateForm'].clearValidate();
          this.$refs['updateForm'].resetFields();
        }
        this.updateForm = {
          BEGINDATE: this.getDay(-30),
          ENDDATE: this.getDay(0)
        },
        this.batchOption = []
      },
      // 获取当天前一天
      getDay(day) {
        var dd = new Date();
        dd.setDate(dd.getDate() + day);
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
        var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
        return y + "" + m + "" + d;
      },
      //  开始日期
      changeStart1() {
        this.pickerOptionsEnd = Object.assign({}, this.pickerOptionsEnd, {
          disabledDate: (time) => {
            if (this.updateForm.BEGINDATE) {
              return time.getTime() < Date.parse(this.updateForm.BEGINDATE.slice(0, 4) + '-' + this.updateForm
                .BEGINDATE.slice(4, 6) +
                '-' + this.updateForm.BEGINDATE.slice(6, 8)) - 8.64e7 || time.getTime() > Date.now();
            } else {
              return time.getTime() > Date.now();
            }
          }
        })
        this.batchQuery()
      },
      // 结束日期
      changeEnd1() {
        this.pickerOptionsStart = Object.assign({}, this.pickerOptionsStart, {
          disabledDate: (time) => {
            if (this.updateForm.ENDDATE) {
              return time.getTime() > Date.parse(this.updateForm.ENDDATE.slice(0, 4) + '-' + this.updateForm
                .ENDDATE
                .slice(4, 6) + '-' +
                this.updateForm.ENDDATE.slice(6, 8)) || time.getTime() > Date.now();
            } else {
              return time.getTime() > Date.now();
            }
          }
        })
        this.batchQuery()
      },
      // 查询业务渠道
      async channelQuery() {
        let resBody = new dictionary();
        resBody.data.FIELDCODE = 'CHANNELTYPE'
        resBody.data.CurrPage = 1
        resBody.data.PageRowNum = 100
        let response = await requestWFM(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.channelOption = response.BODY.FIEDATA;
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      // 查询批次号
      async batchQuery() {
        this.batchOption = []
        let resBody = new batch()
        resBody.data.BEGINDATE = this.updateForm.BEGINDATE
        resBody.data.ENDDATE = this.updateForm.ENDDATE
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.batchOption = response.BODY.RSPSTRUCT
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD
              .ReturnMessage
          })
        }
      },
      openAutoSample() {
        this.batchQuery()
        this.updateIsShow = true
      },
      dataFormat(row, column) {
        if (column.property == "SAMPLFLAG")
          // 0-自动 1-手动
          return row[column.property] == '0' ? '自动' : row[column.property] == '1' ? '手动' : row[column.property];
      },
      // 打开触发自动抽样
      async openUpdate(row,flag) {
        let resBody = new triggerSample()
        if(flag == '0'){
          resBody.data.BATCHDATE = row.OPERAWORKDATE
        }else{
          resBody.data.BATCHDATE = row.BATCHNODATE

        }
        resBody.data.COLLECTFLAG = '1'
        resBody.data.BATCHNO = row.BATCHNO
        resBody.data.CHANNELTYPE = row.CHANNELTYPE
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.$message({
            type: 'success',
            message: '触发成功!'
          });
          this.querySummary()
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD
              .ReturnMessage
          })
        }
      },
      openAutoSummary(row) {
        this.$router.push({
          name: '29204', //有问题！自己配置的改动了就会报错
          params: {
            row: row
          }
        });
      },
      // 查询采
      // async batchQuery() {
      //   let resBody = new batch();
      //   let response = await request(resBody);
      //   if (response.SYS_HEAD.ReturnCode == "000000") {
      //     this.batchOption = response.BODY.RSPSTRUCT;
      //   } else {
      //     this.$message({
      //       type: "error",
      //       message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
      //     })
      //   }
      // },
      //  // 查询业务渠道
      // async channelQuery() {
      //   let resBody = new dictionary();
      //   resBody.data.FIELDCODE = 'CHANNELTYPE'
      //   resBody.data.CurrPage = 1
      //   resBody.data.PageRowNum = 100
      //   let response = await requestWFM(resBody);
      //   if (response.SYS_HEAD.ReturnCode == "000000") {
      //     this.channelOption = response.BODY.FIEDATA;
      //   } else {
      //     this.$message({
      //       type: "error",
      //       message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
      //     })
      //   }
      // },
      // 自动抽样汇总查询
      async querySummary() {
        let resBody = new summary()
        resBody.data.SAMPLFLAG = this.ruleForm.SAMPLFLAG
        resBody.data.BEGINDATE = this.ruleForm.BEGINDATE
        resBody.data.ENDDATE = this.ruleForm.ENDDATE
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.tableData = response.BODY.RSPSTRUCT
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      changePage(val) {
        this.currentPage = val;
        this.querySummary()
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.querySummary()
      },
      // 获取当天前一天
      getDay(day) {
        var dd = new Date();
        dd.setDate(dd.getDate() + day);
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
        var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
        return y + "" + m + "" + d;
      },
      //  开始日期
      changeStart() {
        this.pickerOptionsEnd = Object.assign({}, this.pickerOptionsEnd, {
          disabledDate: (time) => {
            if (this.ruleForm.BEGINDATE) {
              return time.getTime() < Date.parse(this.ruleForm.BEGINDATE.slice(0, 4) + '-' + this.ruleForm
                .BEGINDATE.slice(4, 6) +
                '-' + this.ruleForm.BEGINDATE.slice(6, 8)) - 8.64e7 || time.getTime() > Date.now();
            } else {
              return time.getTime() > Date.now();
            }
          }
        })
      },
      // 结束日期
      changeEnd() {
        this.pickerOptionsStart = Object.assign({}, this.pickerOptionsStart, {
          disabledDate: (time) => {
            if (this.ruleForm.ENDDATE) {
              return time.getTime() > Date.parse(this.ruleForm.ENDDATE.slice(0, 4) + '-' + this.ruleForm
                .ENDDATE
                .slice(4, 6) + '-' +
                this.ruleForm.ENDDATE.slice(6, 8)) || time.getTime() > Date.now();
            } else {
              return time.getTime() > Date.now();
            }
          }
        })
      },
      // 重置查询条件
      clearForm(item) {
        // 重置表格
        this.$refs["" + item].resetFields();
      },
    }
  }

</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
